package cn.itcast.service.impl;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.AddressDao;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    //查询分院列表
    @Override
    public List<Address> findAll() {
        return addressDao.findAll();

    }

    //新增分院
    @Override
    public Result submit(Address address) {
        int submit = addressDao.submit(address);
        if (submit !=1){
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }else {
            return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS);
        }
    }

    //弹出编辑框
    @Override
    public Result findById(Integer id) {
        Address address = addressDao.findById(id);
        if (address == null){
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS,address);
    }

    @Override
    public Result update(Address address) {
        int update = addressDao.update(address);
        if (update != 1){
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
    }

    //删除分院
    @Override
    public Result delete(Integer id) {
        int delete = addressDao.delete(id);
        if (delete != 1){
            return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
    }

    @Override
    public Result find(Integer enable, Integer id) {
        try {
            addressDao.find(enable, id);
            if(enable ==1){
                return new Result(true,MessageConstant.ENABLE_ADDRESS_SUCCESS);
            }else {
                return new Result(true,MessageConstant.ENABLE1_ADDRESS_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ENABLE_ADDRESS_FAIL);
        }
    }
}
